package com.daka.controller;

import com.alibaba.fastjson.JSONObject;
import com.daka.entity.*;
import com.daka.enumerate.StatusCode;
import com.daka.service.*;
import com.daka.tools.AesUtil;
import com.daka.tools.DateUtil;
import com.daka.vo.request.CreateOrderVo;
import com.daka.vo.request.SpreadVo;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.CheckUserVo;
import com.daka.vo.response.DataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author beliveli on 2016/4/7.
 *         用户
 */
@Controller
@RequestMapping(value = "/planB")
@Slf4j
public class PinganPlanbController {

    final String PINGAN_AES_KEY = "29aec3c3f0866c11ac75296766574b1f";

    @Resource
    private PinganPlanbService pinganPlanbService;

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public DataResponseVo<PinganPlanb> create(@RequestBody SpreadVo spreadVo){

        log.info("/planB/create param  ======> " + spreadVo);

        PinganPlanb pinganPlanb = pinganPlanbService.selectByCardNo(spreadVo.getCardNo());
        if(null == pinganPlanb){
            pinganPlanb = new PinganPlanb();
            pinganPlanb.setMobile(spreadVo.getMobile());
            pinganPlanb.setSn("");
            pinganPlanb.setCardno(spreadVo.getCardNo());
            pinganPlanb.setName(spreadVo.getName());
            pinganPlanb.setChannel(spreadVo.getChannel());
            pinganPlanb.setBusiness(spreadVo.getBusiness());
            pinganPlanbService.save(pinganPlanb);
        }else{
            return new DataResponseVo<PinganPlanb>(StatusCode.SERVER_EXCEPTION.getCode(), "已经开过户了");
        }
        return new DataResponseVo<PinganPlanb>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),pinganPlanb);
    }

    @ResponseBody
    @RequestMapping(value = "/callBack", method = RequestMethod.POST)
    public BaseResponseVo callBack(@RequestParam("resultJson") String resultJson) {

        log.info("/spreadInsurance/checkUser param  ======> " + resultJson);

        String resultStr = "";
        try {
            resultStr = AesUtil.decrypt(resultJson, PINGAN_AES_KEY);
        } catch (Exception e) {
            log.error("返回值解密失败 ！");
            e.printStackTrace();
        }
        log.info("/spreadInsurance/checkUser param  解密后 ======> " + resultStr);

        if (StringUtils.isNotEmpty(resultStr)) {
            JSONObject resultObject = JSONObject.parseObject(resultStr);

            if (resultObject.getBoolean("success")) {

                JSONObject bankAccount = resultObject.getJSONObject("obj").getJSONObject("bankAccount");

               PinganPlanb pinganPlanb = pinganPlanbService.selectByCardNo(bankAccount.getString("idNo"));
                if (null != pinganPlanb) {
                    pinganPlanb.setCardnum(bankAccount.getString("bankAcnt"));
                    pinganPlanb.setIsnew(bankAccount.getInteger("isNewCus"));
                    pinganPlanb.setSn("A" + new Date().getTime());

                    pinganPlanbService.updateById(pinganPlanb);
                }
            }
        }
        return new BaseResponseVo(StatusCode.OK.getCode(), StatusCode.OK.getMessage());
    }
}